package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.NotificationQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Notification;
import cn.org.eshow.model.User;
import cn.org.eshow.service.AccessTokenManager;
import cn.org.eshow.service.NotificationManager;
import cn.org.eshow.webapp.action.response.NotificationSimpleResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息通知API接口
 */
@AllowedMethods({"mine"})
public class NotificationAction extends ApiBaseAction<Notification> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private NotificationManager notificationManager;
    @Autowired
    private AccessTokenManager accessTokenManager;

    private Notification notification;
    private List<Notification> notifications = new ArrayList<Notification>();
    private NotificationQuery query = new NotificationQuery(Boolean.TRUE);

    /**
     * 我的消息通知列表
     */
    public void mine() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        Page<Notification> page = notificationManager.search(query);
        notifications = page.getDataList();
        if (notifications.isEmpty()) {
            RenderUtil.noData();
            return;
        }
        List<NotificationSimpleResponse> responses = new ArrayList<NotificationSimpleResponse>();
        for (Notification obj : notifications) {
            responses.add(new NotificationSimpleResponse(obj));
        }
        RenderUtil.page("获取成功", "notifications", page, responses);
    }

    public Notification getNotification() {
        return notification;
    }

    public void setNotification(Notification notification) {
        this.notification = notification;
    }

    public List<Notification> getNotifications() {
        return notifications;
    }

    public void setNotifications(List<Notification> notifications) {
        this.notifications = notifications;
    }

    public NotificationQuery getQuery() {
        return query;
    }

    public void setQuery(NotificationQuery query) {
        this.query = query;
    }

}